package com.yyq.gxdc.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yyq.gxdc.bean.Result;
import com.yyq.gxdc.entity.Coupon;
import com.yyq.gxdc.entity.ReceiveCoupon;
import com.yyq.gxdc.entity.User;
import com.yyq.gxdc.service.ICouponService;
import com.yyq.gxdc.service.IReceiveCouponService;
import com.yyq.gxdc.service.IUserService;
import com.yyq.gxdc.entity.*;
import com.yyq.gxdc.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @version 1.0
 * @create 2020/10/30 20:42
 */
@CrossOrigin
@RestController
@RequestMapping("/receiveCoupon")
public class ReceiveCouponController {

    @Autowired
    private IReceiveCouponService receiveCouponService;
    @Autowired
    private IUserService userService;

    @Autowired
    private ICouponService couponService;



    /**
     *  领取优惠卷
     * @param cid 优惠卷id
     * @param req
     * @return
     */
    @PostMapping(value = "/addReceiveCoupon")
    public Result<Object> addReceiveCoupon(int cid, HttpServletRequest req) {
        ReceiveCoupon receiveCoupon = new ReceiveCoupon();
        receiveCoupon.setCid(cid);
        // 获取当前登录人id
        Object curruser = req.getSession().getAttribute("user");
        if(null == curruser){
            return Result.error("您还未登录！");
        }
        int userId = (int) curruser;
        receiveCoupon.setUid(userId);
        try {
            receiveCouponService.save(receiveCoupon);
            return Result.ok();
        } catch (Exception e) {
            return Result.error("领取失败！");
        }
    }


    /**
     * 我的优惠卷
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/receiveCouponList")
    public Result<IPage<Coupon>> receiveCouponList(
            @RequestParam(defaultValue = "1") Integer pageNo,
            @RequestParam(defaultValue = "10000") Integer pageSize,
            HttpServletRequest req) {
        Result<IPage<Coupon>> result = new Result<IPage<Coupon>>();
        ReceiveCoupon receiveCoupon = new ReceiveCoupon();
        IPage<ReceiveCoupon> allReceiveCoupon =null;
        Object curruser = req.getSession().getAttribute("user");
        Page<ReceiveCoupon> page = new Page<ReceiveCoupon>(pageNo, pageSize);
        // 根据当前登录人 判断 是管理员 还是 非管理员
        if(null == curruser){
            allReceiveCoupon = receiveCouponService.getAllReceiveCoupon(page, receiveCoupon);
        }else{
            User user = userService.getById((Integer)curruser);
            if("1".equals(user.getUsetype())){
                // 如果userTYpe 是1  则是管理员 看到所有的
                allReceiveCoupon = receiveCouponService.getAllReceiveCoupon(page, receiveCoupon);
            }else{
                // 负责 只看到自己的
                receiveCoupon.setUid((int)curruser);
                allReceiveCoupon = receiveCouponService.getAllReceiveCoupon(page, receiveCoupon);
            }
        }

        List<ReceiveCoupon> records = allReceiveCoupon.getRecords();
        // 取出来我的收藏的 行程  查询对应的行程信息
        List<Coupon> couponList = new ArrayList<>();
        if(records.size()>0){
            for (int i = 0; i < records.size() ; i++) {
                ReceiveCoupon receiveObj = records.get(i);
                Coupon byId = couponService.findById(receiveObj.getCid());
                couponList.add(byId);
            }
        }
        IPage<Coupon> routeIPage = new Page<Coupon>();
        routeIPage.setRecords(couponList);
        result.setSuccess(true);
        result.setResult(routeIPage);
        return result;
    }
}
